package com.cn.goodsadmin.observer;

import com.cn.goodsadmin.common.observes.*;
import com.cn.goodsadmin.config.BaseConfig;
import com.cn.goodsadmin.po.*;
import com.cn.goodsadmin.service.AppService;
import com.cn.goodsadmin.service.LogisticsService;
import com.cn.goodsadmin.service.TemplateService;
import com.cn.goodsadmin.util.SendTempUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;


@Component("UpdateOrderObserver")
public class UpdateOrderObserver implements IObservesr {

    @Autowired
    TemplateService templateService;

    @Autowired
    AppService appService;

    @Autowired
    LogisticsService logisticsService;

    @PostConstruct
    public void init() {
        MessageManager.getInstanceMessage().registerMessage(this, MessageConfig.UPDATEORDERSTATUTS);
        System.out.println("init UpdateOrderObserver");
    }

    //  how  validate the  destory method is  a question
    @PreDestroy
    public void cleanUp() {
        MessageManager.getInstanceMessage().unRegisterMessage(this, MessageConfig.UPDATEORDERSTATUTS);
        System.out.println("cleanUp");
    }


    public void onNoticeMessage(Message message) {
        UpdateOrderStatusMessage updateOrderStatusMessage=(UpdateOrderStatusMessage)message;
        final OrderInfo orderInfo=updateOrderStatusMessage.orderInfo;
        final Userreal userreal=updateOrderStatusMessage.userreal;
        final PanicLog panicLog=updateOrderStatusMessage.panicLog;
        TheadPool.messageObserverWorkhreadPoolExecutor.submit(new Runnable() {

            public void run(){
                try{
                    int appid=0;
                    if(orderInfo!=null) {
                        appid = orderInfo.getcAppid();
                    }else if(userreal!=null){
                        appid=userreal.getcAppid();
                    }else if(panicLog!=null){
                        appid=panicLog.getcAppid();
                    }
                    App app = appService.findAppById(appid);
                    if(app!=null) {
                        String realAppid = app.getcAppid();
                        Template template = templateService.findOrderTemplateByAppid(realAppid, BaseConfig.TEMPLATEORDERUPDATE);

                        if (template != null) {
                            if(orderInfo!=null) {
                                Logistics logistics = logisticsService.findLogisticsByLid(orderInfo.getcLogisticsid());
                                SendTempUtil.sendOrderUpdateTips(orderInfo, template, realAppid, logistics);
                            }else if(userreal!=null){
                                Logistics logistics = logisticsService.findLogisticsByLid(userreal.getcLogisticsid());
                                SendTempUtil.sendOrderUpdateTips(userreal, template, realAppid, logistics);
                            }else if(panicLog!=null){
                                Logistics logistics = logisticsService.findLogisticsByLid(panicLog.getcLogisticsid());
                                SendTempUtil.sendOrderUpdateTips(panicLog, template, realAppid, logistics);
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        });
    }
}
